package blog.open1111.controller.admin;

import blog.open1111.entity.ArcType;
import blog.open1111.entity.PageBean;
import blog.open1111.service.ArcTypeService;
import blog.open1111.service.ArticleService;
import blog.open1111.service.impl.InitComponent;
import blog.open1111.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.json.HTTP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ContextLoader;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dengwubo on 2017/9/11.
 */
@Controller
@SuppressWarnings("all")
@RequestMapping("/admin/arcType")
public class ArcTypeAdminController {
    @Autowired
    private ArcTypeService arcTypeService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private InitComponent initComponent;

    @RequestMapping("/list")
    public String list(@RequestParam(value="page",required=false)String page, @RequestParam(value="rows",required=false)String rows,
                       HttpServletResponse response) throws Exception{
        PageBean pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        Map<String,Object> map = new HashMap<>();
        map.put("start",pageBean.getStart());
        map.put("size",pageBean.getPageSize());
        List<ArcType> arcTypeList = arcTypeService.list(map);
        Long total = arcTypeService.getTotal(map);
        JSONObject result = new JSONObject();

        JSONArray jsonArray = JSONArray.fromObject(arcTypeList);

        result.put("rows",jsonArray);
        result.put("total",total);
        ResponseUtil.write(response,result);
        return null;
    }

    @RequestMapping("/save")
    public String save(ArcType arcType, HttpServletResponse response) throws Exception{
        int resultTotal = 0; //操作记录条数
        if (arcType.getId()==null){
            resultTotal = arcTypeService.add(arcType);
        }else{
            resultTotal = arcTypeService.update(arcType);
        }

        JSONObject result = new JSONObject();
        if (resultTotal>0){
            initComponent.refershSystem(ContextLoader.getCurrentWebApplicationContext().getServletContext());
            result.put("success",true);
        }else{
            result.put("success",false);
        }

        ResponseUtil.write(response,result);
        return null;
    }


    @RequestMapping("/delete")
    public String delete(String ids,HttpServletResponse response) throws Exception{
        String [] idsStr = ids.split(",");
        JSONObject result = new JSONObject();
        Arrays.asList(idsStr).forEach(id->{
            if(articleService.getNumByTypeId(Integer.parseInt(id))>0){
                result.put("exist","帖子类别下有帖子,不能删除");
            }else{
                arcTypeService.delete(Integer.parseInt(id));
            }

        });
        initComponent.refershSystem(ContextLoader.getCurrentWebApplicationContext().getServletContext());
        result.put("success",true);
        ResponseUtil.write(response,result);

        return null;
    }




}
